package com.cloud.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间段获取工具类
 *
 * @author yzj
 */
public class DateTimeUtils {

    /**
     * 获取开始年份时间
     */
    public static String getStartYearTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, num);
        Date date = calendar.getTime();
        return format.format(date) + " 00:00:00";
    }

    /**
     * 获取开始月份时间
     */
    public static String getStartMonthTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, num);
        Date date = calendar.getTime();
        return format.format(date) + " 00:00:00";
    }

    /**
     * 获取当天开始时间
     */
    public static String getStartDataTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, num);
        Date date = calendar.getTime();
        return format.format(date) + " 00:00:00";
    }

    /**
     * 获取年份结束时间
     */
    public static String getEndYearTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, num);
        Date date = calendar.getTime();
        return format.format(date) + " 23:59:59";
    }

    /**
     * 获取月份结束时间
     */
    public static String getEndMonthTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, num);
        Date date = calendar.getTime();
        return format.format(date) + " 23:59:59";
    }

    /**
     * 获取当天结束时间
     */
    public static String getEndDataTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, num);
        Date date = calendar.getTime();
        return format.format(date) + " 23:59:59";
    }

    /**
     * 获取自定义小时时间
     */
    public static String getHourDateTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR, num);
        Date date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 获取自定义分钟时间
     */
    public static String setMinuteDateTime(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, num);
        Date date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 获取天时间对象
     */
    public static Calendar getDateObject(int num) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, num);
        return calendar;
    }

    /**
     * 获取天时间并日期格式化
     *
     * @param num
     * @param format
     * @return
     */
    public static String getDateFormat(int num, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Calendar calendar = getDateObject(num);
        Date date = calendar.getTime();
        return dateFormat.format(date);
    }

    /**
     * 设置日期时间格式
     */
    public static String setDateFormat(Date dateTime, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return format.format(dateTime);
    }

    /**
     * 设置日期时间格式
     */
    public static String setDateFormat(String dateTime, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            Date newTime = sdf.parse(dateTime);
            SimpleDateFormat forMat = new SimpleDateFormat(dateFormat);
            return forMat.format(newTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当月第一天
     */
    public static String getMoonFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date date = calendar.getTime();
        return format.format(date) + " 00:00:00";
    }

    /**
     * 获取当月最后一天
     */
    public static String getMoonTheLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        Date date = calendar.getTime();
        return format.format(date) + " 23:59:59";
    }

    /**
     * 获取季度第一天时间
     */
    public static String getSeasonFirstDay() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String dateTime = null;
        if (month >= 1 && month < 4) {
            dateTime = year + "-01-01";
        } else if (month >= 4 && month < 7) {
            dateTime = year + "-04-01";
        } else if (month >= 7 && month < 10) {
            dateTime = year + "-07-01";
        } else {
            dateTime = year + "-10-01";
        }
        return dateTime + " 00:00:00";
    }

    /**
     * 获取季度最后一天
     */
    public static String getSeasonTheLastDay() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String dateTime = null;
        if (month >= 1 && month < 4) {
            dateTime = year + "-03-31";
        } else if (month >= 4 && month < 7) {
            dateTime = year + "-06-31";
        } else if (month >= 7 && month < 10) {
            dateTime = year + "-09-31";
        } else {
            dateTime = year + "-12-31";
        }
        return dateTime + " 23:59:59";
    }

    /**
     * 获取去年的第一天
     */
    public static String getYearFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year - 1);
        Date currYearFirst = calendar.getTime();
        return format.format(currYearFirst) + " 00:00:00";
    }

    /**
     * 获取去年的最后一天
     *
     * @return
     */
    public static String getYearTheLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year - 1);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date date = calendar.getTime();
        return format.format(date) + " 23:59:59";
    }

    /**
     * 获取上n周第一天
     */
    public static String getLastMonday(Integer num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        // 将每周第一天设为星期一,默认是星期天
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -num * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // 设置时间
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return format.format(cal.getTime());
    }

    /**
     * 获取上n周最后一天
     *
     * @param num 上n周
     * @return 日期
     */
    public static String getLastSunday(Integer num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        // 将每周第一天设为星期一，默认是星期天
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -num * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // 设置时间
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);
        return format.format(cal.getTime());
    }

    /**
     * 获取按天日期时间对象
     */
    public static Date getDateTime(int num) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, num);
        return calendar.getTime();
    }

    /**
     * 获取按小时日期时间对象
     */
    public static Date getHourDate(int num) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR, num);
        return calendar.getTime();
    }

    /**
     * 获取当前年,月,日
     */
    public static Integer getCurrentTime(int time) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        return calendar.get(time);
    }

    /**
     * 获取两个时间差（分钟）
     *
     * @param inData  当时前时间
     * @param outData
     * @return
     */
    public static Long getMinutePoor(Date inData, Date outData) {
        long diff = inData.getTime() - outData.getTime();
        long minute = diff / 60 / 1000;
        return minute;
    }

    /**
     * 获取两个时间差（小时）
     *
     * @param inData  当时前时间
     * @param outData
     * @return
     */
    public static Long getHourPoor(Date inData, Date outData) {
        Long day = DateUtil.between(inData, outData, DateUnit.DAY);
        return DateUtil.between(inData, outData, DateUnit.HOUR) - day * 24;
    }

}
